package com.decent.text.pdf.test3;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.BufferedWriter;
import java.io.File;
import java.io.StringWriter;
import java.util.Map;

/**
 * @author 王雪杰
 * @date 2021/11/13 14:53
 */
public class HtmlGenerator {
    public static String generate(String template, Map params) throws Exception{
        Configuration config = new Configuration();
        String templatePath = "D:\\idea\\workspace\\test-4-7\\src\\main\\resources\\pdfTest";
        config.setDirectoryForTemplateLoading(new File(templatePath));
        // Configuration config = FreemarkerConfiguration.getConfiguation();
        config.setDefaultEncoding("UTF-8");
        Template tp = config.getTemplate(template);
        StringWriter stringWriter = new StringWriter();
        BufferedWriter writer = new BufferedWriter(stringWriter);
        tp.setEncoding("UTF-8");
        tp.process(params, writer);
        String htmlStr = stringWriter.toString();
        writer.flush();
        writer.close();
        return htmlStr;
    }
}
